
# REQUIRES: Data/Framing the Exit Experiment Raw Data.csv

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Experiment/Cleaning.R")

################# Figure S1 - Attitudes Toward Withdrawal Across Treatment Conditions #################
### H3a
# H3a: The effect of the enemy victory frame will be more strongly negative (on support for withdrawal) among liberals.
# logit withdraw_yn [interact enemy victory treatment with political ideology] without making liberals a binary variable.
p1<- data[(data$political_affiliation != "8"),]
p1 <- p1[!is.na(p1$id), ]

p1$t_aff <- p1$taliban*p1$political_affiliation

h3a <- glm(withdraw_yn ~ taliban*political_affiliation, data=p1, family="binomial")
summary(h3a)
h3a %>% coef() %>% exp()

logitmfx(withdraw_yn ~ taliban*political_affiliation, data=p1)

# Evaluated at means
allmean <- data.frame(taliban=mean(p1$taliban),
                      political_affiliation=mean(p1$political_affiliation),
                      t_aff=mean(p1$t_aff))
allmean$pred.prob <- predict(h3a, newdata=allmean, type="response")
allmean <- cbind(allmean,predict(h3a, newdata=allmean, type="response", se.fit=TRUE))
allmean

# Evaluated at means of political_affiliation and t_aff by taliban condition
h3a2 <- glm(withdraw_yn ~ taliban_recode+political_affiliation+t_aff, data=p1, family="binomial")
summary(h3a2)
allmean2 <- data.frame(political_affiliation=rep(mean(p1$political_affiliation),2),
                       t_aff=rep(mean(p1$t_aff),2),
                       taliban_recode=as.factor(c('control', 'taliban')))
allmean2 <- cbind(allmean2,predict(h3a2, newdata=allmean2, type="response", se.fit=TRUE))
allmean2

### Figure 3
# Interplot
margin <- unit(0.5, "line")
grob1 <- interplot(m = h3a, var1 = "taliban", var2 = "political_affiliation") +
  theme_light() +
  theme(text=element_text(family="Times New Roman"), axis.text.x=element_blank()) +
  #scale_x_discrete(labels = c("Extemely Liberal", "Liberal", "Slightly Liberal", "Moderate", "Slightly Conservative", "Conservative", "Extremely Conservative")) +
  labs(y = "Marginal Effect on Support for Withdrawal") +
  geom_hline(yintercept = 0, linetype = "dashed")
grob2 <- textGrob("   ", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob3 <- textGrob("Political Ideology", gp=gpar(fontsize=11, fontfamily="Times New Roman"))
grob4 <- textGrob("Figure S1: Interaction of Enemy Victory Frame and Political Ideology", gp=gpar(fontsize=13, fontfamily="Times New Roman"))

plot3 <- grid.arrange(grob1, grob2, grob3, grob4, 
                      heights = unit.c(unit(1,"null"), grobHeight(grob2) + 1.2*margin, grobHeight(grob3) + 1.2*margin, grobHeight(grob4) + margin),
                      vp=viewport(width=0.95, height=0.95))

ggsave("Plots/Figure S1 - Interaction of Enemy Victory Frame and Political Ideology.png", plot3)

rm(allmean, allmean2, grob1, grob2, grob3, grob4, h3a, h3a2, p1, plot3, margin)
